﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace UnionPay4WP
{
    public class errorTranslator
    {
        public errorTranslator()
        {
 
        }

        public static string translate(string input)
        {
            Error err = new Error();
            string result = translate(input,ref err);
            return result;
        }

        public static string successCode()
        {
            string result = "00000";
            Uri uri = new Uri("/UnionPay4WP;component/errors.xml", UriKind.Relative);
            try
            {
                XElement root = XElement.Load(uri.ToString());
                result = root.Element("error").Element("code").Value;
            }
            catch { }
            return result;
        }

        public static string translate(string input,ref Error err)
        {
            if (input == "")
            {
                return "未知错误";
            }
            string result = easyTranslate(input,ref err);
            if ("" == result)
            {
                //if ("991" == input.Substring(0, 3))
                //{
                //    err.TargetUri = result = "CUPS未定义错误";
                //}
                //else if ("992" == input.Substring(0, 3))
                //{
                //    err.TargetUri = result = "MPI未定义错误";
                //}
                //else if ("993" == input.Substring(0, 3))
                //{
                //    err.TargetUri = result = "CSTP未定义错误";
                //}
               
            }
            return result;
            
        }
        private static string easyTranslate(string input,ref Error err)
        {
            string result="";
            if (input == null)
                return "未知错误"; //+ UnionPayControl.order.TransCurrency + "----" + UnionPayControl.order.OrderId;
            Uri uri = new Uri("/UnionPay4WP;component/errors.xml", UriKind.Relative);
            XElement root = XElement.Load(uri.ToString());
            var text = from t in root.Descendants("error").Where(w => w.Element("code").Value==input)  
                       select new Error
                       {
                           Code=t.Element("code").Value,
                           Meaning=t.Element("meaning").Value,
                           TargetUri = t.Element("TargetUri").Value

                       };
            if(null!=text)
            foreach (var a in text)
            {
                result = a.Meaning;
                err = a;
            }
            if (("" == result) && (input.Length > 2))
            {
                result = easyTranslate(input.Substring(0, 2), ref err);
                if (("" == result) && (input.Length == 5))
                    result = easyTranslate("XXXXX", ref err);

            }
            return result;
        }
    }
}
